package com.example;

public class ReverseNodesInKGroup {

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode reverseKGroup(ListNode head, int k) {
        if (head == null) {
            return head;
        }
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        ListNode dummyHead = new ListNode(-1);
        cur = head;
        ListNode prev = null;
        int count = size / k;
        while (count > 0) {
            int temp = k - 1;
            while (temp > 0) {
                ListNode next = head.next;
                cur.next = prev;
                prev = cur;
                cur = next;
            }
            dummyHead.next = prev;

            count--;
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {

        int[] arr1 = {2, 3};
        ListNode node1 = new ListNode(1);
        ListNode temp1 = node1;
        for (int i = 0; i < arr1.length; i++) {
            temp1.next = new ListNode(arr1[i]);
            temp1 = temp1.next;
        }
    }


}
